<script setup lang="ts">
import { ref , nextTick} from 'vue'
import { useUserStore } from '@/store/modules/user-store'
import { getLodop } from '@/lib/lodop/LodopFuncs'
import dayjs from 'dayjs'
import printModal from '@/components/business/print-modal/index.vue'
import { transferOrderApi } from '@/api/business/storage/transfer-api'
import { useTransformSpec } from '@/hooks/web/use-transform-spec'
const { getSpecQuantity } = useTransformSpec()
const emit = defineEmits(['close', 'getList'])
const open = ref<boolean>(false)

const companyInfo = useUserStore().getCompanyInfo

const visible = ref(false)
// 详情
const options = ref([])
const printLog = ref({})
defineExpose({
  showDrawer
})
function showDrawer(item, val) {
  printLog.value = val.printLog
  options.value = item
  visible.value = true
}
function onClose() {
  visible.value = false
}

const loading = ref(false)
const printLoading = ref(false)
const usePrint = () => {
  const onPrint = async () => {
    printLoading.value = true

try {
    const res = await transferOrderApi.detail({ id: options.value.id, is_print: true })

    const newCount = res.data.printLog.print_count
    if (!options.value.printLog) options.value.printLog = {}
    options.value.printLog.print_count = newCount
  } catch (e) {
    console.error('更新打印次数失败', e)
    printLoading.value = false
    return
  }

await nextTick()

    const LODOP = getLodop()
    if (!LODOP) {
      open.value = true
      printLoading.value = false
      return
    }
    LODOP.PRINT_INITA('0mm', '0mm', '241mm', '93mm', '调拨单')
    LODOP.SET_PRINT_PAGESIZE(1, '215.9mm', '93mm', '调拨单')
    LODOP.ADD_PRINT_TABLE(0, '5%', '90%', '100%', document.querySelector('#printTransfer')?.outerHTML)
    LODOP.SET_PRINT_STYLEA(0, 'TableRowThickNess', '12pt')
    LODOP.ADD_PRINT_TEXT(0, '5%', 196, 20, '县链云仓')
    LODOP.SET_PRINT_STYLEA(0, 'ItemType', 0)
    LODOP.NewPageA()
    LODOP.PREVIEW()
    printLoading.value = false
  }
  return {
    onPrint
  }
}
const { onPrint } = usePrint()
const handleModalClose = () => {
  open.value = false
}
</script>

<template>
  <printModal :open="open" @cancel="handleModalClose" />
  <a-drawer title="调拨单打印" :open="visible" @close="onClose">
    <template #closeIcon>
      <close @on-close="onClose" />
    </template>
    <table id="printTransfer" border="0" center cellSpacing="0" cellPadding="1" width="100%" style="border-collapse: collapse; font-size: 11pt">
      <tbody>
        <tr>
   <td colspan="8">
            <div style="line-height: 30px; text-align: center; font-size: 16pt; font-weight: 700">{{ companyInfo.name }}调拨单</div>
            <font style="position: absolute; right: 0; top: 0">
              <span tdata="pageNO" format="#">#</span>
              <span>/</span>
              <span tdata="pageCount" format="#">##</span>
            </font>
          </td>
  </tr>
        </tbody>
      <tbody v-loading="loading">
        <tr>
          <td colspan="5">出货仓库: {{ options?.startW?.name || '-' }}&nbsp;&nbsp;&nbsp;&nbsp;入货仓库: {{ options?.endW?.name || '-' }}&nbsp;&nbsp;&nbsp;&nbsp;调拨日期: {{ options?.create_time }}&nbsp;&nbsp;&nbsp;&nbsp;打印次数: {{ options?.printLog?.print_count || 0 }}&nbsp;&nbsp;&nbsp;&nbsp;打印时间: {{ dayjs().format('YYYY-MM-DD HH:mm:ss') }}&nbsp;&nbsp;&nbsp;&nbsp;单据编号: {{ options?.order_no }}</td>
        </tr>
        <tr>
          <td width="10%" style="border: 1pt solid #000">
            <div align="center"><b>序号</b></div>
          </td>
          <td width="30%" style="border: 1pt solid #000">
            <div align="center"><b>商品名称</b></div>
          </td>
          <td width="10%" style="border: 1pt solid #000">
            <div align="center"><b>数量</b></div>
          </td>
          <td width="20%" style="border: 1pt solid #000">
            <div align="center"><b>单位</b></div>
          </td>
          <td width="20%" style="border: 1pt solid #000">
            <div align="center"><b>备注</b></div>
          </td>
        </tr>
        <tr v-for="(item, index) in options?.detail" :key="item.id">
          <td style="border: 1pt solid #000; text-align: center">{{ index + 1 }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ item.goods?.goods_name || '-' }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ getSpecQuantity(item.transfer_num, item.spec) }}{{ item.spec?.unit_name }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ item.spec?.unit_name }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ item.remark }}</td>
        </tr>
        <tr>
          <td style="border: 1pt solid #000; text-align: center">合计</td>
          <td style="border: 1pt solid #000">&nbsp;</td>
          <td style="border: 1pt solid #000; text-align: center">{{ options?.detail.reduce((prev, item) => prev + getSpecQuantity(item.transfer_num, item.spec), 0) }}</td>
          <td style="border: 1pt solid #000; text-align: center">&nbsp;</td>
          <td style="border: 1pt solid #000">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="10">
            制单：{{ options?.apply_user_name }}&nbsp;&nbsp;&nbsp;&nbsp;开单日期：{{ options?.create_time }}&nbsp;&nbsp;&nbsp;&nbsp;
            <span>备注: {{ options?.remark }}</span>
          </td>
        </tr>
      </tbody>
    </table>
    <template #footer>
      <div class="flex justify-end">
        <a-button type="default" @click="onClose" style="margin-right: 8px">关闭</a-button>
        <a-button :loading="printLoading" type="primary" @click="onPrint">打印</a-button>
      </div>
    </template>
  </a-drawer>
</template>
